<?php
require_once APPPATH.'third_party/PHPExcel/PHPExcel/IOFactory.php';
class Excelproc_model extends Base_Model
{
    public function __construct()
    {
        parent::__construct();
    }

    public function printSheet($input_arr)
    {
        echo count($input_arr).'<hr><hr>';
        foreach ($input_arr as $row){
            foreach ($row as $ck => $cell){
                echo $ck.'=>'.$cell.'&nbsp;&nbsp;|&nbsp;&nbsp;';
            }
            echo '<hr/>';
        }
        exit();
    }
    public function cliPrintSheet($input_arr,$header)
    {
        $rownum=1;
        foreach ($header as $h){
            echo $h.'  ';
        }
        echo PHP_EOL;
        foreach ($input_arr as $row){
            echo $rownum.'  ';
            foreach ($row as $cell){
                echo $cell.'  ';
            }
            echo PHP_EOL;
            $rownum++;
        }
    }

    public function getFmdkData()
    {
        $this->load->model('redis_model');
        $redis=$this->redis_model->getRedisInstance();

        $this->load->database();
        $totaluser=$this->db->from('stat_fmdk')->count_all_results();
        $rank1=round($totaluser*0.05);
        $rank2=round($totaluser*0.15)+$rank1;
        $rank3=round($totaluser*0.35)+$rank2;
        $rankindex=1;

        $limit=10;
        $offset=0;
        $exceldata=array();
        try{
            while (true){
                $this->db->reconnect();
                $res=$this->db->select('f.uid,u.userName,u.telNum,f.time,f.books,f.login,f.shnum,f.qnum,f.ansnum')
                    ->from('stat_fmdk as f')
                    ->join('mod_user as u','u.uid=f.uid','left')
                    ->order_by('f.time DESC ,f.login DESC ,f.books DESC ,f.shnum DESC ,f.qnum DESC ,f.ansnum DESC,f.id ASC')
                    ->limit($limit,$offset)
                    ->get()->result_array();
                if (empty($res)) break;
                foreach ($res as $row){
                    if ($rankindex<$rank1||$rankindex==$rank1){
                        $ranklevel='最强王者';
                    }elseif ($rankindex>$rank1&&$rankindex<$rank2||$rankindex==$rank2){
                        $ranklevel='尊贵铂金';
                    }elseif ($rankindex>$rank2&&$rankindex<$rank3||$rankindex==$rank3){
                        $ranklevel='倔强青铜';
                    }else{
                        $ranklevel='再接再厉';
                    }
                    $excelrow=array();
                    $excelrow['rank']=$rankindex;
                    $excelrow['level']=$ranklevel;
                    $excelrow['userName']=$row['userName'];
                    $excelrow['telNum']=$row['telNum'];
                    $excelrow['time']=$row['time'];
                    $excelrow['books']=$row['books'];
                    $excelrow['login']=$row['login'];
                    $excelrow['shnum']=$row['shnum'];
                    $excelrow['qnum']=$row['qnum'];
                    $excelrow['ansnum']=$row['ansnum'];
                    $exceldata[]=$excelrow;

                    $redis->zAdd('fmdk:20170606:20170724:rank',$rankindex,$row['uid']);

                    echo 'level:'.$ranklevel.' rank:'.$rankindex.' '.$row['uid'];

                    $rankindex++;
                }
                $offset+=$limit;
            }
            array_unshift($exceldata,array('排名','段位','姓名','电话','收听时长(分钟)','收听书数(本)','连续学习(天)','分享次数','提问次数','回答次数'));
            return $exceldata;

        }catch (Exception $e){
            $this->db->close();
            echo $e->getMessage().PHP_EOL;
            exit();
        }

    }

}